/*
 * Copyright (C)1995-2009 Elcom Software
 */
package com.elcom.emocbc.util;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 
 * @author Nguyen Thua Thang
 * 
 */
public class ConfigurationManager {

    public static final String HOME_DIR = System.getProperty("user.home");
    public static final String SEPARATOR = System.getProperty("file.separator");
    private static final String CONFIG_FOLDER = "config/cellbroadcast";
    private static final String DIR = HOME_DIR + SEPARATOR + CONFIG_FOLDER + SEPARATOR;
    private static final ConfigurationManager instance = new ConfigurationManager();
    private static ConcurrentHashMap<String, Properties> cachedProperties = new ConcurrentHashMap<String, Properties>();
    
    private ConfigurationManager() {
    	
    }

    public static ConfigurationManager getInstance() {
        return instance;
    }

	public Properties getProperties(String filename) {
    	if(!cachedProperties.containsKey(filename)) {
    		Properties loaded = loadProperties(filename);    		
          cachedProperties.put(filename, loaded);
    	}
    	return cachedProperties.get(filename);    	
    }

    public static void reloadConfiguration() {
        cachedProperties.clear();
    }

    public void reloadConfiguration(String module) {
        cachedProperties.remove(module);
    }

    private Properties loadProperties(String module) {
    	Properties properties = new Properties();    	
        String filename = DIR + module.toLowerCase() + ".properties";
        String classpathFile = CONFIG_FOLDER + SEPARATOR + module.toLowerCase() + ".properties";
        try {
            FileInputStream pin = new FileInputStream(filename);
            properties.load(pin);
            return properties;        
        } catch (Exception e) {
            
        }
        try {        	        	        	
          InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(classpathFile);
          properties.load(inputStream);
        } catch (Exception e) {
        	
        }
        return properties;
    }
   
    public static String getStrProp(String fileName, String key, String defaultValue) {
    	Properties properties = instance.getProperties(fileName);
    	return properties.getProperty(key, defaultValue);        
    }

    public static  int getIntProp(String fileName, String key, int defaultValue) {
    	Properties properties = instance.getProperties(fileName);
    	return Integer.parseInt(properties.getProperty(key, String.valueOf(defaultValue)));
    }

    public static long getLongProp(String fileName, String key, long defaultValue) {
    	Properties properties = instance.getProperties(fileName);
    	return Long.parseLong(properties.getProperty(key, String.valueOf(defaultValue)));
    }

    public static double getDoubleProp(String fileName, String key,double defaultValue) {
    	Properties properties = instance.getProperties(fileName);
    	return Double.parseDouble(properties.getProperty(key, String.valueOf(defaultValue)));
    }

    public static byte getByteProp(String fileName, String key, byte defaultVal) {
    	Properties properties = instance.getProperties(fileName);
    	return Byte.parseByte(properties.getProperty(key, Byte.toString(defaultVal)));
    }

    public static boolean getBooleanProp(String fileName, String key, boolean defaultVal) {
    	Properties properties = instance.getProperties(fileName);    	
    	return Boolean.parseBoolean(properties.getProperty(key, String.valueOf(defaultVal)));
    }
    
}
